//
//  ViewController.m
//  NewProjectDemo
//
//  Created by mac on 2017/8/2.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ViewController.h"
#import "JftMergeSDK.h"
#import "JFTDES.h"
#import "MBProgressHUD.h"
#import "PayViewController.h"
#import "HLMBProgressHUD.h"
@interface ViewController ()<JftSDKPayDelegate>
@property (weak, nonatomic) IBOutlet UITextField *moneyText;
@property (nonnull,nonatomic,strong) JFTPayModel *model;
@property (strong, nonatomic) NSDateFormatter *formatter;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
}
- (IBAction)weichatPay:(UIButton*)sender {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.model = [JFTPayModel new];
    
    self.model.p1_yingyongnum = @"0401u420170405090645l83fpfCj42";//040108123829C5rcGKDs
    self.model.key            = @"e88d4f6751c6598d4698dc7039437ddb";// 50ec79e2f5b61a500db2077523dc09c6
    self.model.iv             = @"d4acd224d881402e";//2917da6ab41cccf1
    NSDate *p2_orderDate      = [NSDate date];
    self.model.p2_ordernumber = [self.formatter stringFromDate:p2_orderDate];
    self.model.p3_money       = self.moneyText.text;
    self.model.p6_ordertime   = [self.formatter stringFromDate:p2_orderDate];
    if (sender.tag == 1000) {
        self.model.p7_productcode = @"WXSDKHS";
    }
    if (sender.tag == 10000) {
        self.model.p7_productcode = @"ZFBSDK";
    }
    NSString *appKey = @"040109151501gcvOZ9gU";
    NSString *MD5EncryptionString = [JFTDES md532BitUpper:[NSString stringWithFormat:@"%@&%@&%@&%@&%@&%@",self.model.p1_yingyongnum,self.model.p2_ordernumber,self.model.p3_money,_model.p6_ordertime,self.model.p7_productcode,appKey]];
    self.model.p8_sign = MD5EncryptionString;
    self.model.controler    = self;
    self.model.parameterDic = @{@"systemFlag":@"JFTCS"};
    [JfPay payByJftPayModel:self.model delegate:self];

}
- (NSDateFormatter *)formatter{
    if (!_formatter) {
        _formatter = [[NSDateFormatter alloc]init];
        [_formatter setDateFormat:@"yyyyMMddHHmmss"];
    }
    return _formatter;
}

- (IBAction)clean:(id)sender {
    
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"JFTMIWENKEY"];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"JFTPAYURLKEY"];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"JFTBASEURLKEY"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
}
- (IBAction)getPayTypeList:(id)sender {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //  68017104468404
    //  4df3acbf2bcce95d52d0f57c2b7e3570
    //  69fc5704171c8f0f
//    [JfPay getPayTypeListWithApplicationNumber:@"01017106784401" aesKey:@"e817a6f244679e1eee1f7050279b8c74" andAesIv:@"be9e202c46bd33a9" returnDelegate:self];
    
     [JfPay getPayTypeListWithApplicationNumber:@"68017104468404" aesKey:@"4df3acbf2bcce95d52d0f57c2b7e3570" andAesIv:@"69fc5704171c8f0f" returnDelegate:self];
}
- (void)hudHidden{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
}
- (void)getPayTypeListFailure:(NSString *)message{
    NSLog(@"%@",message);
    dispatch_async(dispatch_get_main_queue(), ^{
        [self hudHidden];
    });
    [HLMBProgressHUD addMBProgressHUDinView:self.view hudMode:5 hideDelay:1.2 hudDetailText:message];
}
- (void)getPayTypeListSuccess:(NSArray *)list{
 
    NSLog(@"%@",list);
    __weak typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       PayViewController *payVC = [PayViewController new];
       payVC.parArray = [list copy];
        dispatch_async(dispatch_get_main_queue(), ^{
            payVC.money  = weakSelf.moneyText.text;
            [weakSelf hudHidden];
            [weakSelf.navigationController pushViewController:payVC animated:YES];
        });
    });
}
- (void)openAppSuccessed{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self hudHidden];
    });
    
}
- (void)openAppFailer:(NSString *)failer{
    NSLog(@"%@",failer);
    [self hudHidden];
    dispatch_async(dispatch_get_main_queue(), ^{
        [HLMBProgressHUD addMBProgressHUDinView:self.view hudMode:5 hideDelay:1.2 hudDetailText:failer];
    });
    
}
- (void)jftPayResult:(NSString *)result{
    NSLog(@"---result=%@---",result);
    dispatch_async(dispatch_get_main_queue(), ^{
        [HLMBProgressHUD addMBProgressHUDinView:[UIApplication sharedApplication].keyWindow hudMode:5 hideDelay:1.2 hudDetailText:result];
    });
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}
@end
